/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package business;

import data.Grouptable;
import data.Requirement;
import data.Userinfotable;
import data.Usertable;
import java.security.Principal;
import java.util.ArrayList;
import java.util.Date;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.validation.constraints.Future;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
import java.util.List;
import javax.ejb.Stateless;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import javax.validation.constraints.NotNull;

/**
 *
 * @author stana
 */
@ManagedBean
@SessionScoped
@Stateless
public class CreateMessageBean
{

    @EJB
    private business.RequirementFacade ejbFacade;
    private Requirement requirement;
    private String place;//pro inputText z index.xhtml
    @Future(message = "Vyberte prosím datum z budoucnosti.")
    private Date date;
    @Size(min = 9, max = 9, message = "Chybný formát čísla.")
    @Pattern(regexp = "[0-9]+", message = "Chybný formát čísla.")
    private boolean firstStage;
    private String selectedPlace;//pro ulozeni ktere mesto bylo vybrano z nabidky
    private List foundPlaces;//mesta v nabidce v index.xhtml
    private YahooPlaceFinder placeFinder;
    private List<Place> placesList;//seznam do ktereho se ulozi mesta, ktere najde yahoo
    private String info;//informace o chybach
    @EJB
    private business.UsertableFacade userFacade;
    private Usertable user;
    @EJB
    private UserinfotableFacade userInfoFacade;
    private Userinfotable userInfo;

    
    /** Creates a new instance of RequirementBean */
    public CreateMessageBean()
    {
        placeFinder = new YahooPlaceFinder();
        firstStage = true;
    }

    public Date getDate()
    {
        return date;
    }

    public void setDate(Date date)
    {
        this.date = date;
    }

    public String getPlace()
    {
        return place;
    }

    public void setPlace(String place)
    {
        this.place = place;
    }

    public boolean isFirstStage()
    {
        return firstStage;
    }

    public void setFirstStage(boolean firstStage)
    {
        this.firstStage = firstStage;
    }

    public List getFoundPlaces()
    {
        foundPlaces = new ArrayList<SelectItem>();
        placesList = placeFinder.getPlaces(place);
        int i = 0;
        for (Place p : placesList)
        {
            foundPlaces.add(new SelectItem(i++, getPlaceName(p)));
        }
        return foundPlaces;
    }

    private String getPlaceName(Place p)
    {
        String ret = "";
        if (p.getLine1() == "")
        {
            ret = p.getLine2() + ", " + p.getLine4();
        } else
        {
            ret = p.getLine1() + ", " + p.getLine4();
        }
        return ret;
    }

    public void setFoundPlaces(List foundPlaces)
    {
        this.foundPlaces = foundPlaces;
    }

    public String getSelectedPlace()
    {
        return selectedPlace;
    }

    public void setSelectedPlace(String selectedPlace)
    {
        this.selectedPlace = selectedPlace;
    }

    public String getInfo()
    {
        return info;
    }

    public void setInfo(String info)
    {
        this.info = info;
    }

    public void find()
    {
        getFoundPlaces();
        if (foundPlaces.isEmpty())
        {
            info = "Nebylo nalezeno žádné místo, zkuste upravit zadání.";
        } else
        {
            firstStage = !firstStage;
            info = "";
        }
        //return "/index";
    }

    public void changePlace()
    {
        firstStage = true;
        selectedPlace = null;
        foundPlaces = null;
        place = null;
        placesList = null;
        //return "/index";
    }

    public String confirm()
    {

        Place p = placesList.get(Integer.parseInt(selectedPlace));
        Principal principal = FacesContext.getCurrentInstance().getExternalContext().getUserPrincipal();

        user = userFacade.find(principal.getName()); // Find User by j_username.
        userInfo=userInfoFacade.findByUserId(user.getUserid());
        requirement = new Requirement(date, Long.valueOf(userInfo.getPhoneNumber()), getPlaceName(p), p.getLatitude(), p.getLongitude(), "tomorrow", 0);
        //ejbFacade.edit(requirement);
        ejbFacade.create(requirement);
        //ejbFacade.flush();
        info = "";
        return "/pages/user/CreateSuccess";

    }


}
